using System;
using System.ComponentModel;
using System.Web.UI;

namespace Pegasus.Web.UI
{
	/// <summary>
	/// 
	/// </summary>
	public class PageEx : Page
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="T:PageEx"/> class.
		/// </summary>
		public PageEx()
		{
		}

		/// <summary>
		/// Finds the child control.
		/// </summary>
		/// <param name="controlId">The control id.</param>
		/// <returns></returns>
		public Control FindChildControl( string controlId )
		{
			return FindChildControl( this.Controls, controlId );
		}

		/// <summary>
		/// Finds the child control.
		/// </summary>
		/// <param name="controls">The controls.</param>
		/// <param name="controlId">The control id.</param>
		/// <returns></returns>
		public Control FindChildControl( ControlCollection controls, string controlId )
		{
			foreach( Control control in controls )
			{
				if( control.ID == controlId )
				{
					return control;
				}

				Control subControl = FindChildControl( control.Controls, controlId );
				if( subControl != null )
				{
					return subControl;
				}
			}

			return null;
		}

		/// <summary>
		/// Gets the query parameter.
		/// </summary>
		/// <param name="queryParam">The query param.</param>
		/// <returns></returns>
		public TType GetQueryParameter<TType>( string queryParam )
		{
			return GetQueryParameter<TType>( queryParam, default( TType ) );
		}

		/// <summary>
		/// Gets the query parameter.
		/// </summary>
		/// <param name="queryParam">The query param.</param>
		/// <param name="defaultValue">The default value.</param>
		/// <returns></returns>
		public TType GetQueryParameter<TType>( string queryParam, TType defaultValue )
		{
			try
			{
				// Get the job id that is passed in on the query string.
				string strQuery = Request.QueryString[ queryParam ];
				if( !string.IsNullOrEmpty( strQuery ) )
				{
					TypeConverter returnConverter = TypeDescriptor.GetConverter( typeof( TType ) );
					return (TType) returnConverter.ConvertFrom( strQuery );
				}
			}
			catch( Exception e )
			{
				// TODO: Log exception
				object a = e;
			}

			return defaultValue;
		}

        /// <summary>
        /// Gets the query string in string form.
        /// </summary>
        public string GetQueryString()
        {
            return Request.QueryString.ToString();
        }
		
		/// <summary>
		/// Raises the load event.
		/// </summary>
		/// <param name="args">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected override void OnLoad( EventArgs args )
		{
			base.OnLoad( args );
			
			if( !IsPostBack )
			{
				OnLoadFirstTime( args );
			}
			else
			{
				OnLoadPostBack( args );
			}

			OnLoadFinish( args );
		}

		/// <summary>
		/// Called the load first time the page is loaded (not a post back).
		/// </summary>
		/// <param name="args">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected virtual void OnLoadFirstTime( EventArgs args )
		{
		}

		/// <summary>
		/// Called when the page is posted back to.
		/// </summary>
		/// <param name="args">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected virtual void OnLoadPostBack( EventArgs args )
		{
		}

		/// <summary>
		/// Called at the end of the load process.
		/// </summary>
		/// <param name="args">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected virtual void OnLoadFinish( EventArgs args )
		{
		}
	}
}
